package com.hq.common.activiti.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.repository.ProcessDefinitionQuery;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.activiti.spring.ProcessEngineFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hq.common.activiti.IWorkFlowService;

@Service("workFlowService")
public class WorkFlowService implements IWorkFlowService {

	@Autowired
	ProcessEngineFactoryBean processEngineBean;

	@Autowired
	RepositoryService repositoryService;

	@Autowired
	RuntimeService runtimeService;

	@Autowired
	TaskService taskService;

	@Override
	public List<Map<String, Object>> getProcessDefinition() {
		List<Map<String, Object>> resMaps = new ArrayList<Map<String, Object>>();
		try {
			ProcessEngine processEngine = processEngineBean.getObject();
			RepositoryService repoService = processEngine.getRepositoryService();
			ProcessDefinitionQuery proDefQuery = repoService.createProcessDefinitionQuery();
			List<ProcessDefinition> defList = proDefQuery.list();
			for (ProcessDefinition def : defList) {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("deployId", def.getDeploymentId());
				map.put("id", def.getId());
				map.put("key", def.getKey());
				map.put("name", def.getName());
				resMaps.add(map);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return resMaps;
	}

	@Override
	public Map<String, Object> initProcessByKey(String key, String userId) {

		// 设置启动人,将之保存在流程变量中
		Map<String, Object> variables = new HashMap<>();
		variables.put("initUser", userId);

		ProcessInstance pi = runtimeService.startProcessInstanceByKey(key,variables);

		Map<String, Object> resMap = new HashMap<String, Object>();
		resMap.put("id", pi.getId());
		resMap.put("name", pi.getName());

		return resMap;
	}

	@Override
	public List<Map<String, Object>> getTaskByInstanceId(String id) {
		List<Task> taskList = taskService.createTaskQuery().processInstanceId(id).list();
		List<Map<String, Object>> resMaps = new ArrayList<Map<String, Object>>();
		for (Task task : taskList) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("taskId", task.getId());
			map.put("taskName", task.getName());
		}
		return resMaps;
	}

	@Override
	public Map<String, Object> initProcessByKey(String key,Map<String, Object> variables) {
		// 设置启动人,将之保存在流程变量中
		ProcessInstance pi = runtimeService.startProcessInstanceByKey(key,variables);

		Map<String, Object> resMap = new HashMap<String, Object>();
		resMap.put("id", pi.getId());
		resMap.put("name", pi.getName());

		return resMap;
	}

}
